<template>
    <el-dialog
            title="选择组卷方式"
            :close-on-click-modal="false"
            :visible.sync="visible" width="60%">
        <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="100px" class="demo-ruleForm"
                 style="margin-top: 20px">
            <el-form-item label="试卷名称" prop="name">
                <el-input v-model="dataForm.name" style="width: 500px"></el-input>
            </el-form-item>
            <el-form-item label="试卷分类" prop="cateId">
                <el-select v-model="dataForm.cateId" filterable placeholder="请选择">
                    <el-option
                            v-for="item in cateList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="组卷方式">
                <el-radio-group v-model="dataForm.compType">
                    <el-radio-button label="0">选题组卷</el-radio-button>
                    <el-radio-button label="1">抽题组卷</el-radio-button>
                    <el-radio-button label="2">随机组卷</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        cateList: [],
        loading: false,
        dataForm: {
          name: '',
          cateId: '',
          compType: 0
        },
        rules: {
          name: [
            {required: true, message: '试卷名称不能为空', trigger: 'blur'}
          ],
          cateId: [
            {required: true, message: '分类不能为空', trigger: 'blur'}
          ],
          compType: [
            {required: true, message: '组卷方式不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init () {
        this.getCateList()
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 获取数据列表
      getCateList () {
        this.$http({
          url: this.$http.adornUrl('/exam-admin/paper/cate/select'),
          method: 'post'
        }).then(data => {
          if (data) {
            this.cateList = data.data
          } else {
            this.cateList = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (parseInt(this.dataForm.compType) === 0) {
              this.$router.push({
                name: 'paper-add',
                query: {
                  name: this.dataForm.name,
                  cateId: this.dataForm.cateId,
                  compType: this.dataForm.compType,
                  createExam: 0
                }
              })
            } else {
              this.$router.push({
                name: 'random-paper-add',
                query: {
                  name: this.dataForm.name,
                  cateId: this.dataForm.cateId,
                  compType: this.dataForm.compType,
                  createExam: 0
                }
              })
            }
            this.loading = false
          }
        })
      }
    }
  }
</script>
